import re

from tortoise import fields
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise.models import Model
from tortoise.validators import RegexValidator
from pydantic import BaseModel


class BaseMixin(Model):
    created_at = fields.DatetimeField(auto_now_add=True, description='创建时间')
    modified_at = fields.DatetimeField(auto_now=True, description='修改时间')
    enabled = fields.BooleanField(default=0, description="是否通过申请")
    reserve = fields.CharField(max_length=50, null=True, description="保留")

    class Meta:
        abstract = True


class Opotions(Model):
    name = fields.CharField(max_length=50, unique=True, description='名称')
    fid = fields.IntField(max_length=5, null=True, description='父id')
    created_at = fields.DatetimeField(auto_now_add=True, description='创建时间')


class StaffMember(BaseMixin):
    department = fields.CharField(max_length=50, description='部门')
    sub_department = fields.CharField(max_length=50, description='子部门')
    name = fields.CharField(max_length=50, description='姓名')
    gender = fields.CharField(max_length=2, description='性别')
    position = fields.CharField(max_length=50, description='单位职务')
    identity = fields.CharField(max_length=50, unique=True, description='身份证号',
                                validators=[RegexValidator(r'(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)', re.I)])
    phone = fields.CharField(max_length=50, unique=True, description='电话',
                             validators=[RegexValidator(r'^(\+?0?86\-?)?1[3-9]\d{9}$', re.I)])

    class Meta:
        table = "staff_member"
        table_description = '工作人员'


class IsCommonMixin:
    is_department_level = fields.BooleanField(description="是否厅级领导")
    is_colonel = fields.BooleanField(description="是否团长")
    is_liaison = fields.BooleanField(description="是否联络员")
    is_help_repre = fields.BooleanField(description="是否农业农村部定点帮扶县代表")
    is_regional_repre = fields.BooleanField(description="是否三区三洲脱贫地区代表")


class Guest(StaffMember):
    activity = fields.CharField(max_length=100, null=True, description='拟参加活动')
    identity_type = fields.CharField(max_length=10, description='证件类型')
    identity = fields.CharField(max_length=50, unique=True, description='证件号')
    is_department_level = fields.BooleanField(description="是否厅级领导")
    is_colonel = fields.BooleanField(description="是否团长")
    is_liaison = fields.BooleanField(description="是否联络员")
    is_help_repre = fields.BooleanField(description="是否农业农村部定点帮扶县代表")
    is_regional_repre = fields.BooleanField(description="是否三区三洲脱贫地区代表")

    class Meta:
        table = "guest"
        table_description = '嘉宾'


class Exhibitor(StaffMember):
    activity = fields.CharField(max_length=100, description='拟参加活动')
    identity_type = fields.CharField(max_length=10, description='证件类型')
    identity = fields.CharField(max_length=50, unique=True, description='证件号')
    position = fields.CharField(max_length=50, description='企业名称及职务')
    address = fields.CharField(max_length=50, description='企业地址')
    product = fields.CharField(max_length=50, description='主要产品')
    is_help_repre = fields.BooleanField(description="是否农业农村部定点帮扶县代表")
    is_regional_repre = fields.BooleanField(description="是否三区三洲脱贫地区代表")
    is_leader_enterprise = fields.BooleanField(description="是否国家级农业产业化重点龙头企业")
    is_certified_enterprise = fields.BooleanField(description="是否获得“有机农产品、农产品地理标志”认证的省级农业产业化重点龙头企业")

    class Meta:
        table = "exhibitor"
        table_description = '参展商'


class Volunteer(StaffMember):
    class Meta:
        table = "volunteer"
        table_description = '志愿者'


class Constructor(StaffMember):
    class Meta:
        table = "constructor"
        table_description = '施工人员'


class Other(StaffMember):
    class Meta:
        table = "other"
        table_description = '其他'


exclude_fields = ('id', 'created_at', 'modified_at', 'enabled')
parameters = {'exclude': exclude_fields}
StaffMemberIn = pydantic_model_creator(StaffMember, name="StaffMemberIn", **parameters)
GuestIn = pydantic_model_creator(Guest, name="GuestIn", **parameters)
ExhibitorIn = pydantic_model_creator(Exhibitor, name='ExhibitorIn', **parameters)
VolunteerIn = pydantic_model_creator(Volunteer, name='VolunteerIn', **parameters)
ConstructorIn = pydantic_model_creator(Constructor, name='ConstructorIn', **parameters)
OtherIn = pydantic_model_creator(Other, name='OtherIn', **parameters)
